//
//  MenuSelectScene.cpp
//  Jump
//
//  Created by Zach Wang on 14-5-15.
//
//
#include "MenuSelectScene.h"
#include "MenuPlayLayer.h"
#include "TransitionCustom.h"
#include "MenuSettingLayer.h"

USING_NS_CC;

Scene* MenuSelectScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = MenuSelectScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MenuSelectScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    
    Sprite* bg = Sprite::create("bg.jpg");
    bg->setAnchorPoint(Point(0.5f, 0.5f));
    bg->setPosition(Point(visibleSize.width/2, visibleSize.height/2));
    this->addChild(bg);
    
    layerManager = (LayerSwitcher*)LayerSwitcher::create(MenuSettingLayer::create(),MenuPlayLayer::create(),nullptr);
    this->addChild(layerManager);
    
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    auto touchListener = EventListenerTouchOneByOne::create();
    
    touchListener->onTouchMoved = [=](Touch* touch,Event* event)
    {
        
        if (!this->touched) {
            int count = layerManager->getLayerCount();
            int now = layerManager->getLayerIndex();
            if(touch->getStartLocation().x<touch->getLocation().x-50){
                if (now <= 0) {
                    return;
                }
                layerManager->switchToWithSectorTransition(0);
            }else if(touch->getStartLocation().x>touch->getLocation().x+50){

                if (now >= count - 1) {
                    return;
                }
                layerManager->switchToWithSectorTransition(1);
            }else{
                return;
            }

            this->touched = true;

        }
    };
    
    touchListener->onTouchBegan = [=](Touch* touch,Event* event)
    {
        this->touched = false;
        return true;
    };
    
    touchListener->onTouchEnded = [=](Touch* touch,Event* event)
    {
        touched = false;
    };
    
    dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    
    return true;
}
